//
//  HistoryHeaderView.swift
//  TinyShopKeeper
//
//  Created by rimi on 16/6/2.
//  Copyright © 2016年 yuying. All rights reserved.
//

import UIKit

class HistoryHeaderView: UITableViewHeaderFooterView {
    
    
 
    var dataSourceOfHistoryHeaderView:[String:AnyObject]? {
        didSet {
            loadHeaderViewdata()
        }
    }
    
    let headerView:HeaderView
    override init(reuseIdentifier: String?) {
        
        let view = NSBundle.mainBundle().loadNibNamed("HeaderView", owner: nil, options: nil).first as! HeaderView
        
        view.frame = CGRectMake(0, 0, ScreenWidth*0, 0)
        self.headerView = view
        super.init(reuseIdentifier: reuseIdentifier!)
        self .addSubview(view)
        
        
        
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    

    func loadHeaderViewdata(){
       let timeStr = self.self.dataSourceOfHistoryHeaderView!["date"]! as! String
        
        
        let dayStr = timeStr.substringFromIndex((timeStr.startIndex.advancedBy(8)))
        let YAndMStr = timeStr.substringToIndex((timeStr.startIndex.advancedBy(7)))
        self.headerView.day.text = dayStr+"日"
        self.headerView.year_m.text = YAndMStr
        self.headerView.week.text = dateBecomeWeek(timeStr)
        self.headerView.receivable.text = self.dataSourceOfHistoryHeaderView!["orderInfo"]!["goods_amount"]?!.stringValue
        self.headerView.RealPrice.text = self.dataSourceOfHistoryHeaderView!["orderInfo"]!["cash_money"]?!.stringValue
        
        
    }
    
    //日期变星期
    func dateBecomeWeek(date:String) -> String{
        
        let arrWeek = ["星期日","星期一","星期二","星期三","星期四","星期五","星期六"]
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        let thatDate = formatter.dateFromString(date)
        let calendar = NSCalendar.init(calendarIdentifier: NSCalendarIdentifierChinese)
        let week = calendar?.component(NSCalendarUnit.Weekday, fromDate: thatDate!)
       
        return arrWeek[week!-1]
        
        
    }
    
}


